package org.springforge.forgeweb.mvc;


import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.naming.NamingException;
import javax.servlet.http.HttpSession;

import org.springforge.forgemailinglist.ForgeMailingList;
import org.springforge.ldap.service.LdapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class MailingListController {
    
    @Autowired
    protected LdapService ldapService = null;
    @Autowired
    protected ForgeMailingList mailingList = null;
	
    @RequestMapping(value="/mailing/index.html")
    public void index(HttpSession session,@RequestParam(value="obj",required=false) String objet, @RequestParam(value="corps",required=false) String corps, Model model){
    try {	
    	String exp = (String) session.getAttribute("user");
		String dest = null;
		dest= ldapService.getDestList();
		if(dest==null)
    		dest="echec ldap.getdestlist()";
		model.addAttribute("exp", exp);
		model.addAttribute("dest", dest);
		if(objet!=null && corps!=null)
    		mailingList.sendMail(objet,corps,exp,dest);
    } catch (NamingException e) {e.printStackTrace();}
      catch (AddressException e) {model.addAttribute("erreur", e);}
      catch (MessagingException e) {model.addAttribute("erreur", e);} 
    }

}
